using System.Collections;
using Script.CreatureEditor;
using UnityEngine;

public class SkinnedCollisionHelper : MonoBehaviour
{
	public SkinnedMeshRenderer rend;

	public EditorController editorController;

	private CWeightList[] nodeWeights;

	private Vector3[] newVert;

	private Mesh mesh;

	private MeshCollider collide;

	private IEnumerator Start()
	{
		yield return new WaitForEndOfFrame();
		yield return new WaitForEndOfFrame();
		collide = GetComponent(typeof(MeshCollider)) as MeshCollider;
		if (collide != null && rend != null)
		{
			Mesh sharedMesh = rend.sharedMesh;
			mesh = new Mesh();
			mesh.vertices = sharedMesh.vertices;
			mesh.uv = sharedMesh.uv;
			mesh.triangles = sharedMesh.triangles;
			newVert = new Vector3[sharedMesh.vertices.Length];
			nodeWeights = new CWeightList[rend.bones.Length];
			for (short num = 0; num < rend.bones.Length; num++)
			{
				nodeWeights[num] = new CWeightList();
				nodeWeights[num].transform = rend.bones[num];
			}
			for (short num = 0; num < sharedMesh.vertices.Length; num++)
			{
				BoneWeight boneWeight = sharedMesh.boneWeights[num];
				if (boneWeight.weight0 != 0f)
				{
					Vector3 p = sharedMesh.bindposes[boneWeight.boneIndex0].MultiplyPoint3x4(sharedMesh.vertices[num]);
					nodeWeights[boneWeight.boneIndex0].weights.Add(new CVertexWeight(num, p, boneWeight.weight0));
				}
				if (boneWeight.weight1 != 0f)
				{
					Vector3 p = sharedMesh.bindposes[boneWeight.boneIndex1].MultiplyPoint3x4(sharedMesh.vertices[num]);
					nodeWeights[boneWeight.boneIndex1].weights.Add(new CVertexWeight(num, p, boneWeight.weight1));
				}
				if (boneWeight.weight2 != 0f)
				{
					Vector3 p = sharedMesh.bindposes[boneWeight.boneIndex2].MultiplyPoint3x4(sharedMesh.vertices[num]);
					nodeWeights[boneWeight.boneIndex2].weights.Add(new CVertexWeight(num, p, boneWeight.weight2));
				}
				if (boneWeight.weight3 != 0f)
				{
					Vector3 p = sharedMesh.bindposes[boneWeight.boneIndex3].MultiplyPoint3x4(sharedMesh.vertices[num]);
					nodeWeights[boneWeight.boneIndex3].weights.Add(new CVertexWeight(num, p, boneWeight.weight3));
				}
			}
		}
		else
		{
			Debug.LogError(base.gameObject.name + ": SkinnedCollisionHelper: this object either has no SkinnedMeshRenderer or has no MeshCollider!");
		}
		UpdateCollisionMesh();
		editorController.bodyBounds = new Vector2(collide.bounds.min.y, 2.1f);
	}

	public void UpdateCollisionMesh()
	{
		if (!(mesh != null))
		{
			return;
		}
		for (int i = 0; i < newVert.Length; i++)
		{
			newVert[i] = new Vector3(0f, 0f, 0f);
		}
		CWeightList[] array = nodeWeights;
		foreach (CWeightList cWeightList in array)
		{
			foreach (CVertexWeight weight in cWeightList.weights)
			{
				newVert[weight.index] += cWeightList.transform.localToWorldMatrix.MultiplyPoint3x4(weight.localPosition) * weight.weight;
			}
		}
		for (int k = 0; k < newVert.Length; k++)
		{
			newVert[k] = base.transform.InverseTransformPoint(newVert[k]);
		}
		mesh.vertices = newVert;
		mesh.RecalculateBounds();
		collide.sharedMesh = mesh;
	}
}
